package controller

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"time"
	"vip-project/model/mysql"
	"vip-project/pkg"
	"vip-project/request"
)

//注册

func Register(c *gin.Context) {
	var registerReq *request.RegisterReq
	if err := c.ShouldBindJSON(&registerReq); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "服务器内部错误",
			"err": err.Error(),
		})
		return
	}
	data := mysql.User{
		Username: registerReq.Username,
		Password: registerReq.Password,
		Level:    registerReq.Level,
		Name:     registerReq.Name,
		Sex:      registerReq.Sex,
		Email:    registerReq.Email,
		Money:    registerReq.Money,
		Score:    registerReq.Score,
		Phone:    registerReq.Phone,
		Status:   registerReq.Status,
	}
	err := data.Register()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "vip注册失败",
			"err": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "vip注册成功",
		"data": data,
	})
}

//列表

func List(c *gin.Context) {
	var listReq *request.ListReq
	if err := c.ShouldBindJSON(&listReq); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "服务器内部错误",
			"err": err.Error(),
		})
		return
	}
	data := mysql.User{Username: listReq.Username}
	_, err := data.List()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "vip列表查询失败",
			"err": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "vip列表查询成功",
		"data": data,
		"time": time.DateTime,
	})
}

//登录

func Login(c *gin.Context) {
	var loginReq *request.LoginReq
	if err := c.ShouldBindJSON(&loginReq); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "服务器内部错误",
			"err": err.Error(),
		})
		return
	}
	data := mysql.User{Username: loginReq.Username}
	err := data.Login()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "登录失败",
			"err": err.Error(),
		})
		return
	}
	t := pkg.NewJWT("2206a")
	token := pkg.CustomClaims{
		ID:       data.ID,
		NickName: data.Username,
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400,
			Subject:   "登录失败",
		},
	}
	createToken, err := t.CreateToken(token)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "token生成失败",
			"err": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "登录成功",
		"data": createToken,
	})
}

//修改

func Update(c *gin.Context) {
	var updateReq *request.UpdateReq
	if err := c.ShouldBindJSON(&updateReq); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "服务器内部错误",
			"err": err.Error(),
		})
		return
	}
	data := mysql.User{
		Username: updateReq.Username,
	}
	err := data.Update(updateReq.Username)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "修改失败",
			"err": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "修改成功",
		"data": data,
	})
}

//删除

func Delete(c *gin.Context) {
	var deleteReq *request.DeleteReq
	if err := c.ShouldBindJSON(&deleteReq); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "服务器内部错误",
			"err": err.Error(),
		})
		return
	}
	data := mysql.User{
		Model: gorm.Model{ID: uint(deleteReq.Id)},
	}
	err := data.Delete(int(data.ID))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"msg": "删除失败",
			"err": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "删除成功",
		"data": data,
	})
}
